package com.sy.cloud.guess.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sy.cloud.guess.model.Count;
import com.sy.common.service.ITestService;
import com.sy.common.util.Funutil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mac on 2018/1/5.
 */
@Controller
@Slf4j
@RequestMapping("/captData")
public class QqDataController {

    @Resource
    ITestService testService;



    @RequestMapping(value = "/qq", method = RequestMethod.GET)
    public void qq(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setLocale(new java.util.Locale("zh","CN"));
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        Map<String,String> params1 = new HashMap<>();
        params1.put(HttpHeaders.REFERER, "https://mma.qq.com/mqqactivity/cgi/starmap/get_online" );
        String result = Funutil.restRequest("https://mma.qq.com/mqqactivity/cgi/starmap/get_online", params,params1);

        Map mapType = JSON.parseObject(result,Map.class);
        Count count = new Count();
        if( mapType.containsKey("msg") ){
            if( mapType.get("msg").equals("success") ){
                String data = mapType.get("data").toString();
                Map mapType1 = JSON.parseObject(data,Map.class);
                count.setCount( mapType1.get("count").toString() );
                count.setSavetime( System.currentTimeMillis()/1000+"" );
                testService.addCount(count);
            }
        }
    }
}
